package com.example.schoolexam2.service.impl;

import com.example.schoolexam2.entity.Professional;
import com.example.schoolexam2.mapper.PowerMapper;
import com.example.schoolexam2.mapper.ProfessionalMapper;
import com.example.schoolexam2.service.ProfessionalService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProfessionalServiceImpl implements ProfessionalService {

    @Autowired
    ProfessionalMapper professionalMapper;
    @Autowired
    PowerMapper powerMapper;

    @Override
    public void readAndSave() throws IOException {
        HSSFWorkbook sheets = new HSSFWorkbook(new FileInputStream("C:/Users/AWei/Desktop/professional.xls"));
        HSSFSheet sheetAt = sheets.getSheetAt(0);
        //循环每一行
        for(int nowNum=3;nowNum<=sheetAt.getLastRowNum();nowNum++){
            Professional professional = new Professional();
            professional.setProfessionalId(change(sheetAt,nowNum,0));
            professional.setProfessionalName(change(sheetAt,nowNum,2));
            professional.setDepartmentName(change(sheetAt,nowNum,8));
            professional.setLevel(change(sheetAt,nowNum,6));
            String idByName = powerMapper.findIdByName(professional.getDepartmentName());
            professional.setProfessionalId(idByName);
            professionalMapper.save(professional);
        }

    }
    String change(HSSFSheet sheet,int row,int cell){
        HSSFCell cell1 = sheet.getRow(row).getCell(cell);
        return cell1.getStringCellValue();
    }
}
